package com.epam.iunna.bookshop.model.service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.epam.iunna.bookshop.model.Item;
import com.epam.iunna.bookshop.model.ItemDao;
import com.epam.iunna.bookshop.model.Order;
import com.epam.iunna.bookshop.model.OrderDao;
import com.epam.iunna.bookshop.model.OrderItem;
import com.epam.iunna.bookshop.model.OrderItemDao;

public class BasketBean implements Serializable{
    
    private static final long serialVersionUID = 1L;
    
    public static final String BASKET_BEAN_SESSION_BEAN = "BASKET_BEAN_SESSION_BEAN";
    
    
    private List<ItemBean> itemList = new LinkedList<ItemBean>();
    private Order order = new Order();
    private BigDecimal totalCost= BigDecimal.ZERO;
    
    public BasketBean(Long userID){
        order.setDate( new Date( new java.util.Date().getTime()) );
        order.setPaymentStatus(false);
        order.setUser(userID);
    }
    
    public void addItemToBasket(Item item) throws SQLException{
        //LOG.debug(this,"fdsfd");
        System.out.println("Adding item to basket " + item.getId() + ":" + item.getName());
        if( item.getQuantityAvaliable() > 0 ){
            for(ItemBean itemBean : itemList){
                if( itemBean.getItem().getId().equals( item.getId() ) ){
                    if( item.getQuantityAvaliable() - itemBean.getQuantity() > 0  ){
                        itemBean.increaseQuantity();
                        reserveItem(itemBean.getItem());
                        recalculateState();
                    }
                    return;
                }
            }
        itemList.add( new ItemBean(item) );
        
        reserveItem(item);
        recalculateState();
        }
    }
    
    private void reserveItem(Item item) throws SQLException{
        item.setQuantityAvaliable(item.getQuantityAvaliable() -1 );
//        ItemDao itemDao = new ItemDao();
//        itemDao.updateQuantityAvaliable(item);
    }
    
    private void unReserveItem(Item item) throws SQLException{
        item.setQuantityAvaliable(item.getQuantityAvaliable() +1 );
    }
    
    public void removeItemFromBasket(Long itemID) throws SQLException{
        System.out.println("Removing item to basket " + itemID);
        for(Iterator<ItemBean> itemIter = itemList.iterator();itemIter.hasNext();){
            ItemBean itemBean = itemIter.next();
            if( itemID.equals(itemBean.getItem().getId())){
                System.out.println("Removing" + itemBean.getItem().getId());
                itemBean.decreaseQuantity();
                unReserveItem(itemBean.getItem());
                
                if( itemBean.getQuantity() <=0 ){
                    itemIter.remove();
                }
            }
        }
        recalculateState();
    }
    
    private void recalculateState(){
        totalCost= BigDecimal.ZERO;
        for(Iterator<ItemBean> itemIter = itemList.iterator();itemIter.hasNext();){
            ItemBean itemBean =  itemIter.next();
            
            totalCost = totalCost.add(itemBean.getItem().getPrice().multiply(new BigDecimal(itemBean.getQuantity())));
        }
        
    }
    
    public void saveOrder() throws SQLException{
        OrderItemDao orderItemDao = new OrderItemDao();
        
        OrderDao orderDao = new OrderDao();
        if( order.getId() == null){
            //new order
            Long orderID = orderDao.addOrder( order );
            order.setId(orderID);
        }else{
            orderDao.updatePaymentStatus(order);
            orderItemDao.delByOrderID(order.getId());
            //delete prev items from order
        }
        List<OrderItem> orderItemList = new LinkedList<OrderItem>();
        ItemDao itemDao = new ItemDao();
        for(ItemBean itemBean : itemList){
            OrderItem orderItem = new OrderItem();
            orderItem.setItemId(itemBean.getItem().getId());
            orderItem.setOrderId(order.getId());
            orderItem.setQuantity(itemBean.getQuantity());
            
            orderItemList.add(orderItem);
            
            itemDao.updateQuantityAvaliable( itemBean.getItem() );
        }
        
        orderItemDao.addOrderItemList(orderItemList);
        
       
//      
        
    }
    
    public void restoreOrderById(Long orderID) throws SQLException{
        OrderDao dao = new OrderDao();
        this.order = dao.getById(orderID);
        
        ItemDao itemDao = new ItemDao();
        List<ItemBean> orderedItemList = itemDao.getListByOrderID( orderID );
        
        itemList = orderedItemList;
        
        recalculateState();
    }

    public List<ItemBean> getItemList() {
        return itemList;
    }

   
    public Order getOrder() {
        return order;
    }

   

    public BigDecimal getTotalCost() {
        return totalCost;
    }

   
    
    

}
